//
//  ChatViewController.m
//  XXX
//
//  Created by Xiaoshiheng_pro on 2017/2/17.
//  Copyright © 2017年 Xiaoshiheng_pro. All rights reserved.
//

#import "ChatViewController.h"
#import "GCDAsyncSocket.h"
#import "Client.h"
#import "UIViewController+ImagePicker.h"

@interface ChatViewController ()<GCDAsyncSocketDelegate,ClientDelegate>

@property (weak, nonatomic) IBOutlet UITextField *messageTF;
@property (weak, nonatomic) IBOutlet UITextView *showMessageTF;
@property (weak, nonatomic) IBOutlet UIImageView *showMessageImage;

@property (nonatomic) Client  *Socket;
//客户端socket
@property (nonatomic) GCDAsyncSocket *clinetSocket;

@end

@implementation ChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.Socket = [Client ClientInit];
    self.Socket.delegate = self;
    self.clinetSocket = self.Socket.clinetSocke;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


- (IBAction)addImage:(id)sender {
    
    [self openChoiceWithCompleteBlock:^(NSDictionary *info){
        
        
        UIImage *image  = [info objectForKey:UIImagePickerControllerEditedImage];
        
        NSData *imageData = UIImagePNGRepresentation(image);
        NSData *sendingData = [self prepareData:imageData type:@"image"];
        //tag： 消息标记
        [self.clinetSocket writeData:sendingData withTimeout:-1 tag:0];
        
    }];
}
//发送消息
- (IBAction)sendMessageAction:(id)sender {
    
    NSData *data = [self.messageTF.text dataUsingEncoding:NSUTF8StringEncoding];
    NSData *sendingData = [self prepareData:data type:@"text"];
    //    //withTimeout -1 :无穷大
    //    //tag： 消息标记
    [self.clinetSocket writeData:sendingData withTimeout:-1 tag:0];

}


//接收消息
- (IBAction)receiveMessageAction:(id)sender {
    
    ///读取数据超时的时候调用
    [self.clinetSocket readDataWithTimeout:11 tag:0];
}

- (void)showMessageWithStr:(NSString *)str{
    self.showMessageTF.text = [self.showMessageTF.text stringByAppendingFormat:@"%@\n", str];
}

- (void)clientSocket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port;{
    
    [self showMessageWithStr:@"链接成功"];
    [self showMessageWithStr:[NSString stringWithFormat:@"服务器IP ： %@", host]];
    
}
- (void)clientSocketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
    
    [self showMessageWithStr:@"链接断开"];
}

- (void)clientSocket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
    
    NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    if ([dic[@"type"] isEqualToString:@"text"]) {
        NSData *textData = [dic objectForKey:@"data"];
        NSString *text = [[NSString alloc] initWithData:textData encoding:NSUTF8StringEncoding];
        [self showMessageWithStr:text];
    }else{
        NSData *imageData = [dic objectForKey:@"data"];
        UIImage *img = [UIImage imageWithData:imageData];
        self.showMessageImage.image = img;
    }
}

-(void)clientSocket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
    
    NSLog(@"发送成功%ld",tag);
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
